package com.swpu.cbg.cnshop.adapter;

import android.content.Context;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.swpu.cbg.cnshop.R;
import com.swpu.cbg.cnshop.bean.Address;

import java.util.List;


public class AddressAdapter extends BaseAdapter<Address,BaseViewHolder> {



    private  AddressLisneter lisneter;
    public AddressAdapter(Context context, List<Address> datas,int itemview,AddressLisneter lisneter) {
        super(context, datas,itemview);

        this.lisneter = lisneter;


    }


    public String replacePhoneNum(String phone){

        return phone.substring(0,phone.length()-(phone.substring(3)).length())+"****"+phone.substring(7);
    }

    @Override
    void bindData(BaseViewHolder holder, final int position) {
        holder.getTextView(R.id.txt_name).setText(mDatas.get(position).getConsignee());
        holder.getTextView(R.id.txt_phone).setText(replacePhoneNum(mDatas.get(position).getPhone()));
        holder.getTextView(R.id.txt_address).setText(mDatas.get(position).getAddr());

        final CheckBox checkBox = (CheckBox) holder.getView(R.id.cb_is_defualt);

        final boolean isDefault = mDatas.get(position).getIsDefault();
        checkBox.setChecked(isDefault);


        if(isDefault){
            checkBox.setText("默认地址");
        }
        else{

            checkBox.setClickable(true);

            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(isChecked && lisneter !=null){

                        mDatas.get(position).setIsDefault(true);
                        lisneter.setDefault(mDatas.get(position));
                    }
                }
            });


        }

    }


    public interface AddressLisneter{


        public void setDefault(Address address);

    }



}
